<template>
  <div>
    <!-- 顶部导航 -->
    <van-nav-bar
      title="登录"
      class="page-nav-bar"
    >
      <van-icon
        slot="left"
        name="cross"
        class="left-icon"
        @click="$router.go(-1)"
      />
    </van-nav-bar>
    <!-- form表单 -->
    <van-form
      @submit="onSubmit"
      ref='loginRef'
    >
      <van-field
        v-model="queryInfo.mobile"
        name="mobile"
        placeholder="请输入手机号"
        type="number"
        maxlength="11"
        :rules="userFormRules.mobile"
      >
        <!-- 具名插槽 -->
        <i
          slot=left-icon
          class='toutiao toutiao-shouji'
        ></i>
      </van-field>
      <van-field
        v-model="queryInfo.code"
        name="code"
        type="number"
        maxlength="6"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
      >
        <template #button>
          <!-- 倒计时插槽 -->
          <van-count-down
            @finish='isShowCountDown=false'
            :time="time"
            format=" ss s"
            v-if="isShowCountDown"
          />
          <van-button
            v-else
            slot=button
            size="small"
            class="send-sms-btn"
            round
            native-type="button"
            @click="clickCode"
          >发送验证码
          </van-button>
        </template>

        <i
          slot=left-icon
          class='toutiao toutiao-yanzhengma'
        ></i>
      </van-field>
      <!-- 登录按钮 -->
      <div class="login-btn-wrap">
        <van-button
          round
          block
          type="info"
          native-type="submit"
        >登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendMessage } from '@/api/user.js'
export default {
  data () {
    return {
      // 控制倒计时显示与隐藏的变量
      isShowCountDown: false,
      // 倒计时变量
      time: 60 * 1000,
      // 登录表单发送给服务器的参数
      queryInfo: {
        mobile: '',
        code: ''
      },
      // 登录表单的校验规则
      userFormRules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '手机号码格式不正确' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式为6位数字' }
        ]
      }

    }
  },
  methods: {
    // 验证码点击处理函数
    async clickCode () {
      // 进行校验
      try {
        await this.$refs.loginRef.validate('mobile')
      } catch (err) {
        return
      }
      // 校验成功
      this.$toast.loading({
        message: '发送中...',
        duration: 0, // 持续展示 toast
        forbidClick: true
      })

      // 发送验证码的请求
      try {
        // 发送请求
        await sendMessage(this.queryInfo.mobile)
        // 让倒计时按钮显示
        this.isShowCountDown = true
        // 弹窗提醒  发送成功
        this.$toast.success('发送成功')
      } catch (err) {
        // 发送失败走这里
        if (err.response && err.response.status === 429) {
          return this.$toast.fail('发送频繁')
        }
        this.$toast.fail('系统错误')
      }
    },
    // 登录按钮处理函数
    async onSubmit () {
      this.$toast.loading({
        message: '加载中...',
        duration: 0, // 持续展示 toast
        forbidClick: true
      })
      // 发起请求
      try {
        const { data: res } = await login(this.queryInfo)
        this.$toast.success('登录成功')
        // 跳转到我的页面
        this.$router.push('/profile')
        // 将数据保存到vuex中
        this.$store.commit('initUser', res.data)
      } catch (err) {
        if (err.response && err.response.status === 400) {
          return this.$toast.fail('亲,账号或用户名错误')
        }
        this.$toast.fail('系统异常,登录失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
// 字体图标
.toutiao {
  font-size: 34px;
}
// 验证码按钮
.send-sms-btn {
  width: 152px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
  border: 0;
}
// 登录盒子
.login-btn-wrap {
  margin: 53px 28px;
  .van-button {
    width: 694px;
    height: 88px;
    background-color: #6db4fb;
    border: 0;
    border-radius: 10px;
  }
}
</style>
